/*
 * @Author: 肖思汗 
 * @Date: 2025-08-22 23:33:36 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-08-23 20:55:27
 */
import react from '@vitejs/plugin-react'
import vitePluginImp from 'vite-plugin-imp'
import { fileURLToPath, URL } from 'url';
import dayjs from "dayjs";
import type { UserConfigExport, ConfigEnv } from 'vite';
import { viteServiceRuntimeTransform } from './vite-service-runtime-transform';

// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfigExport => {

    const isDev = (mode === "development");
    const isTest = (mode === "test");
    const isProd = (mode === "production");

    const nowDate = dayjs().format("YYYY-MM-DD HH:mm:ss");

    return ({
        base: "./",
        // 全局替换
        define: {
            "process.env.NODE_ENV": `'${mode}'`,
            "process.env.BUILD_TIME": `'${nowDate}'`,
            "process.env.oss_host": `'https://www-3dkeshihua.oss-cn-shanghai.aliyuncs.com'`,
        },
        optimizeDeps: {
            include: [], // 根据自己的需求添加需要保留的模块名
            exclude: ['pg-hstore'], // 排除pg-hstore依赖
        },
        build: {
            outDir: 'dist',
            assetsInlineLimit: 1024 * 0, // 10k 以下的图片转成 base64
            minify: 'terser',
            terserOptions: {
                compress: {
                    drop_console: false,
                    drop_debugger: true,
                }
            },
            chunkSizeWarningLimit: 1024 * 100,
            rollupOptions: {
                input: {
                    "index": fileURLToPath(new URL('./index.html', import.meta.url)),
                    "admin": fileURLToPath(new URL('./admin.html', import.meta.url)),
                    // "mobile_upload": fileURLToPath(new URL('./mobile_upload.html', import.meta.url))
                },
                external: ['pg-hstore'], // 构建时排除pg-hstore
            }
        },
        plugins: [
            // Service运行时代码转换插件 - 运行时自动转换Service代码
            viteServiceRuntimeTransform(),
            // 配置 React 插件，确保生产环境正确处理 JSX 转换
            react({
                // 配置 babel 转换选项
                babel: {
                    // 明确指定 React 运行时，避免 jsxDEV 错误
                    presets: [
                        ['@babel/preset-react', {
                            runtime: 'automatic',
                            development: isDev,
                            importSource: 'react'
                        }]
                    ]
                }
            }),
            vitePluginImp({
                optimize: true,
                libList: [
                    {
                        libName: 'antd',
                        libDirectory: 'es',
                        style: (name) => `antd/es/${name}/style`
                    },
                    // {
                    //     libName: 'antd-mobile',
                    //     style: () => false,
                    //     libDirectory: 'es/components',
                    //     replaceOldImport: true
                    // }
                ]
            })
        ],
        resolve: {
            alias: {
                "@": fileURLToPath(new URL('./', import.meta.url)),
            }
        },
        css: {
            preprocessorOptions: {
                less: {
                    javascriptEnabled: true,// 支持内联 javascript
                    // 禁用空规则集警告
                    warning: {
                        "empty-rules": false
                    }
                },
            }
        },
        // 热更新时清空控制台
        clearScreen: true,
        server: {
            port: 5222,
            proxy: {
                '/taro_vite_react_mobx_node_express_sequelize_api': {
                    target: 'http://localhost:81',
                    changeOrigin: true,
                    // rewrite: (path) => path.replace(/^\/api\//, '/api/')
                },
            }
        }
    })

}
